package view.manager;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import base.NumberGeneration;
import dao.CourseDao;
import dao.MajorDao;
import dao.ManagerDao;
import model.Course;
import model.Department;
import model.Major;
import model.Manager;
import util.StringUtil;

import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;

public class CourseAddInterfrm extends JInternalFrame {
	private JTextField cou_id;
	private JTextField cou_name;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField cou_credit;
	private JComboBox comboBox;
	private String cou_category = "选修";
	private char category = '1';

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CourseAddInterfrm frame = new CourseAddInterfrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CourseAddInterfrm() {
		setTitle("课程添加");
		setIconifiable(true);
		setClosable(true);
		setBounds(100, 100, 450, 300);

		JLabel lblNewLabel = new JLabel("课程编号：");
		lblNewLabel.setIcon(new ImageIcon(CourseAddInterfrm.class.getResource("/images/num.png")));

		cou_id = new JTextField();
		cou_id.setEditable(false);
		cou_id.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("课程名称：");
		lblNewLabel_1.setIcon(new ImageIcon(CourseAddInterfrm.class.getResource("/images/name.png")));

		cou_name = new JTextField();
		cou_name.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("所属专业：");
		lblNewLabel_2.setIcon(new ImageIcon(CourseAddInterfrm.class.getResource("/images/describe.png")));

		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (comboBox.getSelectedItem() instanceof Major) {
					cou_id.setText(NumberGeneration.createCourseNum((Major)comboBox.getSelectedItem()));
				}else {
					cou_id.setText("");
				}
			}
		});

		JLabel lblNewLabel_3 = new JLabel("课程类别：");
		lblNewLabel_3.setIcon(new ImageIcon(CourseAddInterfrm.class.getResource("/images/设施类别定义.png")));

		JRadioButton rdbtnNewRadioButton = new JRadioButton("选修");
		rdbtnNewRadioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cou_category = "选修";
				category = '1';
				if (comboBox.getSelectedItem() instanceof Major) {
					cou_id.setText(NumberGeneration.createCourseNum((Major)comboBox.getSelectedItem()));
				}else {
					cou_id.setText("");
				}
			}
		});
		rdbtnNewRadioButton.setSelected(true);
		buttonGroup.add(rdbtnNewRadioButton);

		JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("必修");
		rdbtnNewRadioButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cou_category = "必修";
				category = '0';
				if (comboBox.getSelectedItem() instanceof Major) {
					cou_id.setText(NumberGeneration.createCourseNum((Major)comboBox.getSelectedItem()));
				}else {
					cou_id.setText("");
				}
			}
		});
		buttonGroup.add(rdbtnNewRadioButton_1);

		JLabel lblNewLabel_4 = new JLabel("课程学分：");
		lblNewLabel_4.setIcon(new ImageIcon(CourseAddInterfrm.class.getResource("/images/学分.png")));

		cou_credit = new JTextField();
		cou_credit.setColumns(10);

		JButton btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (StringUtil.isEmpty(cou_id.getText())) {
					JOptionPane.showMessageDialog(null, "请选择专业", "提示", JOptionPane.ERROR_MESSAGE);
					cou_id.requestFocus();
					return;
				}
//				if (!StringUtil.checkNumChar(cou_id.getText())) {
//					JOptionPane.showMessageDialog(null, "课程编号需为数字或字母组成", "提示", JOptionPane.ERROR_MESSAGE);
//					cou_id.requestFocus();
//					return;
//				}
//				if (cou_id.getText().length() >= 9) {
//					JOptionPane.showMessageDialog(null, "课程编号太长", "提示", JOptionPane.ERROR_MESSAGE);
//					cou_id.requestFocus();
//					cou_id.selectAll();
//					return;
//				}
//				if (res > 0) {
//					JOptionPane.showMessageDialog(null, "课程编号已存在", "提示", JOptionPane.ERROR_MESSAGE);
//					cou_id.requestFocus();
//					cou_id.selectAll();
//					return;
//				}
				
				
				if (StringUtil.isEmpty(cou_name.getText())) {
					JOptionPane.showMessageDialog(null, "课程名称不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					cou_name.requestFocus();
					return;
				}
				if (cou_name.getText().length() >= 30) {
					JOptionPane.showMessageDialog(null, "课程名称太长", "提示", JOptionPane.ERROR_MESSAGE);
					cou_name.requestFocus();
					cou_name.selectAll();
					return;
				}
				if (StringUtil.isEmpty(cou_credit.getText())) {
					JOptionPane.showMessageDialog(null, "课程学分不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					cou_credit.requestFocus();
					return;
				}
				if (!StringUtil.isNum(cou_credit.getText()) || Integer.valueOf(cou_credit.getText()) >= 10
						|| Integer.valueOf(cou_credit.getText()) <= 0) {
					JOptionPane.showMessageDialog(null, "课程学分需为数字（0-10）", "提示", JOptionPane.ERROR_MESSAGE);
					cou_credit.requestFocus();
					cou_credit.selectAll();
					return;
				}
				String m_id;
				Major major = (Major) comboBox.getSelectedItem();
				m_id = major.getM_id();
				// String cou_id, String cou_category, String cou_name, String cou_credit,
				// String m_id
				if (CourseDao.add(new Course(cou_id.getText(), cou_category, cou_name.getText(), cou_credit.getText(),
						m_id)) > 0) {
					JOptionPane.showMessageDialog(null, "添加成功", "提示", JOptionPane.INFORMATION_MESSAGE);
					comboBox.setSelectedIndex(0);
					rdbtnNewRadioButton.setSelected(true);
					cou_credit.setText("");
					cou_name.setText("");
				}
			}
		});
		btnNewButton.setIcon(new ImageIcon(CourseAddInterfrm.class.getResource("/images/add.png")));

		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				comboBox.setSelectedIndex(0);
				cou_id.setText("");
				cou_name.setText("");
				cou_credit.setText("");
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(CourseAddInterfrm.class.getResource("/images/resets.png")));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(92)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_3)
									.addGap(18)
									.addComponent(rdbtnNewRadioButton)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(rdbtnNewRadioButton_1))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_4)
									.addGap(18)
									.addComponent(cou_credit))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_1)
									.addGap(18)
									.addComponent(cou_name, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel)
									.addGap(18)
									.addComponent(cou_id, GroupLayout.PREFERRED_SIZE, 157, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_2)
									.addGap(18)
									.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 155, GroupLayout.PREFERRED_SIZE))))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(113)
							.addComponent(btnNewButton)
							.addGap(34)
							.addComponent(btnNewButton_1)))
					.addContainerGap(83, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(25)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(cou_id, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(cou_name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_3)
						.addComponent(rdbtnNewRadioButton)
						.addComponent(rdbtnNewRadioButton_1))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_4)
						.addComponent(cou_credit, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addContainerGap(21, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);

		fillDepartmentType();
	}

	/**
	 * 初始化下拉框
	 */
	private void fillDepartmentType() {
		ArrayList<Major> arrayList = MajorDao.select();
		comboBox.addItem("请选择系");
		for (Major major : arrayList) {
			comboBox.addItem(major);
		}
	}
}
